package com.shengqi.xinnongren.work.ui.user.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.user.model.WalletDetailBean;
import com.shengqi.xinnongren.work.ui.user.view.WalletDetailView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/7/27.
 */
public class WalletDetailPresenter extends BasePresenter<WalletDetailView> {
    public void getList(int pageIndex) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageIndex);
        map.put("size", 15);
        JSONObject jsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getBalanceDetail(requestBody), respond -> {
            getView().hideLoadingView();
            HttpRespond<String> httpResult = Global.getHttpResult(respond);
            if (httpResult.result == 1) {
                List<WalletDetailBean> list = new Gson().fromJson(httpResult.data, new TypeToken<List<WalletDetailBean>>() {
                }.getType());
                getView().showBalanceDetailRes(list);
            } else
                getView().onRequestFailed(httpResult.message);

        });
    }
}
